package com.bytech.common.biz.entity.base;

import java.io.Serializable;

import com.bytech.common.biz.exception.BlogApiGlobalRuntimeException;
import lombok.*;

/**
 * @Author zhao
 * @Create 2021/7/13 17:45
 */
@Data
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = -1L;

    private Integer code;
    private String msg;
    private boolean success;
    private Boolean failure;
    private T data;

    public static <T> BaseResultBuilder<T> error(String msg) {
        return BaseResult.<T>builder().success(false).code(-1).msg(msg);
    }

    public static <T> BaseResultBuilder<T> error(BlogResultStatus status) {
        return BaseResult.<T>builder().success(false).code(status.code).msg(status.message);
    }

    public static <T> BaseResultBuilder<T> error(BlogApiGlobalRuntimeException exception) {
        return exception == null ? error("服务器遇到了未知错误!")
                : BaseResult.<T>builder().success(false).code(exception.getResultStatus().code).msg(exception.getMessage());
    }

    public static <T> BaseResultBuilder<T> success() {
        return BaseResult.<T>builder().success(true).code(0);
    }

    public static <T> BaseResultBuilder<T> success(String msg) {
        return BaseResult.<T>builder().success(true).code(0).msg(msg);
    }

    public boolean isSuccess() {
        return success;
    }

    public boolean isFailure() {
        return !success;
    }

    public Boolean getFailure() {
        return !success;
    }

    public Boolean getSuccess() {
        return success;
    }
}
